package com.longkier.gx_library_springboot.repository;

import com.longkier.gx_library_springboot.entity.BookAdmin;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface BookAdminRepository extends JpaRepository<BookAdmin, Long> {

    Optional<BookAdmin> findByUsername(String username);
    Optional<BookAdmin> findByEmail(String email);

    @Modifying
    @Query("DELETE FROM BookAdmin ba WHERE ba.bookAdminId IN :bookAdminId")
    int deleteBookAdminAllByIdInBatch(@Param("bookAdminId") List<Long> bookAdminId);

    // 新增模糊查询方法
    @Query("SELECT ba FROM BookAdmin ba " +
            "WHERE (:status IS NULL OR ba.status = :status) " +
            "AND (:username IS NULL OR ba.username LIKE %:username%) " +
            "AND (:bookAdminName IS NULL OR ba.bookAdminName LIKE %:bookAdminName%) " +
            "AND (:email IS NULL OR ba.email LIKE %:email%)")
    Page<BookAdmin> findBookAdminByByCriteria(@Param("status") Integer status,
                              @Param("username") String username,
                              @Param("bookAdminName") String bookAdminName,
                              @Param("email") String email,
                              Pageable pageable);
}
